package com.atguigu.service;

import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @DubboReference
    private AdminService adminService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询登录的用户
        Admin admin = adminService.getByUsername(username);
        if(null == admin){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //将登录的用户信息封装进入一个User
        User user = new User(username,admin.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList(""));
        return user;
    }
}
